package com.studor.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.studor.common.Result;
import com.studor.entity.Board;
import com.studor.service.BoardService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/api/board")
public class BoardController {

    @Autowired
    private BoardService boardService;

    @GetMapping("/list")
    @SaCheckLogin
    public Result<List<Board>> list() {
        List<Board> boards = boardService.list();
        return Result.success(boards);
    }

    @GetMapping("/page")
    @SaCheckLogin
    public Result<IPage<Board>> page(@RequestParam(defaultValue = "1") Integer current,
                                     @RequestParam(defaultValue = "10") Integer size) {
        Page<Board> page = new Page<>(current, size);
        IPage<Board> result = boardService.page(page);
        return Result.success(result);
    }

    @PostMapping("/add")
    @SaCheckLogin
    public Result<Void> add(@RequestBody Board board) {
        board.setTime(LocalDateTime.now());
        boardService.save(board);
        return Result.success("添加公告成功");
    }

    @PutMapping("/update")
    @SaCheckLogin
    public Result<Void> update(@RequestBody Board board) {
        boardService.updateById(board);
        return Result.success("更新公告成功");
    }

    @DeleteMapping("/delete/{id}")
    @SaCheckLogin
    public Result<Void> delete(@PathVariable String id) {
        boardService.removeById(id);
        return Result.success("删除公告成功");
    }
}
